package com.imu.wys.service.impl;

import com.imu.wys.mapper.UserLogMapper;
import com.imu.wys.mapper.UserMapper;
import com.imu.wys.model.User;
import com.imu.wys.model.UserLog;
import com.imu.wys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserLogMapper userLogMapper;

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public int insertUser(User user) {
        int t = userMapper.insertUser(user);
        return t;
    }

    @Override
    public int numAction(String username, String useraction) {
        return userLogMapper.numAction(username,useraction);
    }
    
    @Override
    public void insertUserLog(User user, String action) {
        //获取系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String action_time = df.format(new Date());
        String userIP = "0.0.0.0";
        //获取ip
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            userIP = ip4.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        UserLog userLog = new UserLog();
        userLog.setUserid(user.getUserid());
        userLog.setUsername(user.getUsername());
        userLog.setUserIP(userIP);
        userLog.setAction(action);
        userLog.setAction_time(action_time);
        userLogMapper.insertUserLog(userLog);
    }


}
